package com.kaikeba.wx.controller;

import com.kaikeba.bean.Courier;
import com.kaikeba.bean.Message;
import com.kaikeba.bean.User;
import com.kaikeba.exception.DuplicateCodeException;
import com.kaikeba.mvc.ResponseBody;
import com.kaikeba.service.CouriorService;
import com.kaikeba.service.UserService;
import com.kaikeba.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UserController {

    @ResponseBody("/wx/loginSms.do")
    public String SendSms(HttpServletRequest request, HttpServletResponse response){
        String userPhone = request.getParameter("userPhone");
//        String  code = RandomUtil.getCode()+"";
//        boolean b = SMSUtil.loginSMS(userPhone, code);
        boolean b = true;
        String code = "123456";
        Message msg = new Message();
        if(b){
            msg.setStatus(0);
            msg.setResult("成功发送");
        }else {
            msg.setStatus(-1);
            msg.setResult("发送失败");
        }
        UserUtil.setLoginSms(request.getSession(),userPhone,code);

        String s = JSONUtil.toJSON(msg);
        return s;
    }

    @ResponseBody("/wx/login.do")
    public String Login(HttpServletRequest request, HttpServletResponse response){
        String userPhone = request.getParameter("userPhone");
        String userCode = request.getParameter("code");
        String sysCode = UserUtil.getLoginSms(request.getSession(), userPhone);
        Message msg = new Message();
        User user = new User();
        if(sysCode==null){
            //说明该手机号并未获取到短信
            msg.setStatus(-1);
            msg.setResult("手机号并未获取到短信");
        }else if(sysCode.equals(userCode)){//短信验证成功
            //号码正确，则添加进去
            user.setPhoneNumber(userPhone);
            msg.setStatus(0);
            //根据手机号查找user和courier，匹配是用户还是快递员
            // 不是快递员，就是用户
            //根据号码查询数据库,下面对象均可能为空
//            System.out.println("UserService"+UserService.findByNumber(userPhone));
//            System.out.println("CouriorService"+CouriorService.findByNumber(userPhone));
           if(UserService.findByNumber(userPhone)!=null){
               UserUtil.setWxUser(request.getSession(),user);
               msg.setResult("用户登录成功");
               user.setUser(true);
           }
           if(CouriorService.findByNumber(userPhone)!=null){
               UserUtil.setWxUser(request.getSession(),user);
               msg.setResult("快递员登录成功");
               user.setUser(false);
           }
            if(CouriorService.findByNumber(userPhone)==null && UserService.findByNumber(userPhone)==null){
                // 注册操作
                try {
                    UserService.insert(user);
                } catch (DuplicateCodeException e) {
                    e.printStackTrace();
                }
                UserUtil.setWxUser(request.getSession(),user);
                msg.setResult("用户登录成功");
                user.setUser(true);
            }
        }else{
            //验证码不一致
            msg.setStatus(-1);
            msg.setResult("验证码不一致");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }


    @ResponseBody("/wx/logout.do")
    public String LogOut(HttpServletRequest request, HttpServletResponse response){
        //销毁Session
        request.getSession().invalidate();
        Message msg = new Message();
        msg.setStatus(0);
        return JSONUtil.toJSON(msg);
    }

    /**
     * 专门用于获取用户身份
     * @param request
     * @param response
     * @return
     */
    @ResponseBody("/wx/userInfo.do")
    public String UserInfo(HttpServletRequest request, HttpServletResponse response){
        User wxUser = UserUtil.getWxUser(request.getSession());
        //System.out.println("userInfo查询出来的wxUser："+wxUser.toString());
        Message msg = new Message();
        msg.setResult(wxUser.getPhoneNumber());
        if(wxUser.isUser()){
            msg.setStatus(0);//是用户
        }else{
            msg.setStatus(-1);//是快递员
        }
        return JSONUtil.toJSON(msg);
    }



}
